//
//  AmountSettingViewController.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/26.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "AmountSettingViewController.h"
#import "HttpServer.h"
#import <TSMessageView.h>
#import "WebViewController.h"
#import <MobClick.h>
#import <BeeCloud.h>
#import "PayButtonViewController.h"
@interface AmountSettingViewController ()<BeeCloudDelegate,PayButtonViewControllerDelegate>

@end

@implementation AmountSettingViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"金额设置页面"];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"金额设置页面"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
        UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(goToMain)];
    [BeeCloud setBeeCloudDelegate:self];
    self.navigationItem.leftBarButtonItem = leftBar;
    [self.navigationController setNavigationBarHidden:NO];
    if ([self.type isEqualToString:@"encharge"])
    {
        [self.completeButtonView setTitle:@"确认充值" forState:UIControlStateNormal];
    }
    else
    {
        [self.completeButtonView setTitle:@"确认提现" forState:UIControlStateNormal];
    }
    [BeeCloud setBeeCloudDelegate:self];
    // Do any additional setup after loading the view from its nib.
}
-(void)goToMain
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view.window endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)completeButtonView:(UIButton *)sender
{
    if ([self.amountTextField.text isEqualToString:@""])
    {
        [TSMessage showNotificationInViewController:self title:@"金额不能为空!" subtitle:nil type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
    }
    else
    {
        BOOL isInt = [self isPureInt:self.amountTextField.text];
        if (isInt == YES)
        {
            NSInteger money = [self.amountTextField.text integerValue]*100;
            if ([self.type isEqualToString:@"encharge"])
            {
                PayButtonViewController *payView = [[PayButtonViewController alloc]init];
                payView.delegate = self;
                [self presentViewController:payView animated:YES completion:nil];
            }
            else
            {
                [HttpServer applyDrawCashWithMoney:money withPassword:nil WithSuccessBlock:^(NSDictionary *responseData)
                 {
                     NSLog(@"%@",responseData);
                     [TSMessage showNotificationInViewController:self title:@"提现申请发送成功!" subtitle:@"请等待客服与您联系" type:TSMessageNotificationTypeSuccess duration:2.0 canBeDismissedByUser:true];
                     
                 }
                 withFailBlock:^(id errorMsg)
                 {
                     NSLog(@"%@",errorMsg);
                     [TSMessage showNotificationInViewController:self title:@"提现申请失败!" subtitle:errorMsg[@"msg"] type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
                 }];
            }
        }
        else
        {
            [TSMessage showNotificationInViewController:self title:@"金额格式错误!" subtitle:nil type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
        }
    }
    
}
- (BOOL)isPureInt:(NSString*)string
{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}
- (void)onBeeCloudResp:(BCBaseResp *)resp
{
    switch (resp.type)
    {
        case BCObjsTypePayResp:
        {
            // 支付请求响应
            BCPayResp *tempResp = (BCPayResp *)resp;
            if (tempResp.resultCode == 0)
            {
                BCPayReq *payReq = (BCPayReq *)resp.request;
                //百度钱包比较特殊需要用户用获取到的orderInfo，调用百度钱包SDK发起支付
                if (payReq.channel == PayChannelBaiduApp && ![BeeCloud getCurrentMode])
                {
                }
                else
                {
                    //微信、支付宝、银联支付成功
                    [TSMessage showNotificationInViewController:self title:resp.resultMsg subtitle:nil type:TSMessageNotificationTypeSuccess duration:2.0 canBeDismissedByUser:true];
                }
            }
            else
            {
                //支付取消或者支付失败
                [TSMessage showNotificationInViewController:self title:[NSString stringWithFormat:@"%@ : %@",tempResp.resultMsg, tempResp.errDetail] subtitle:nil type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
            }
        }
            break;
        case BCObjsTypeQueryBillsResp:
        {
        }
            break;
        case BCObjsTypeQueryRefundsResp:
        {
        }
            break;
            
        case BCObjsTypeOfflinePayResp:
        {
        }
            break;
        case BCObjsTypeOfflineBillStatusResp:
        {
            
        }
            break;
        case BCObjsTypeOfflineRevertResp:
        {

        }
            break;
        default:
        {
            if (resp.resultCode == 0)
            {
                [TSMessage showNotificationInViewController:self title:resp.resultMsg subtitle:nil type:TSMessageNotificationTypeSuccess duration:2.0 canBeDismissedByUser:true];
            }
            else
            {
                [TSMessage showNotificationInViewController:self title:[NSString stringWithFormat:@"%@ : %@",resp.resultMsg, resp.errDetail] subtitle:nil type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
            }
        }
            break;
    }
}

-(void)payMoney
{
    NSInteger money = [self.amountTextField.text integerValue]*100;
    
    [HttpServer inpoursWithMoney:money WithSuccessBlock:^(NSDictionary *responseData)
    {
        NSLog(@"%@",responseData);
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
        BCPayReq *payReq = [[BCPayReq alloc] init];
        payReq.channel = PayChannelAliApp;
        payReq.title = @"billTitle";
        payReq.totalFee = [NSString stringWithFormat:@"%d",[responseData[@"money"] intValue]];
        payReq.billNo = responseData[@"outTradeNo"];
        payReq.scheme = @"payDemo";
        payReq.billTimeOut = 360;
        payReq.viewController = self;
        payReq.optional = dict;
        [BeeCloud sendBCReq:payReq];
    }
        withFailBlock:^(id errorMsg)
    {
        NSLog(@"%@",errorMsg);
        [TSMessage showNotificationInViewController:self title:@"充值失败!" subtitle:errorMsg[@"msg"] type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
    }];
}

@end
